using System.Collections.Generic;

using Microsoft.SharePoint;

namespace RealDolmen.SharePoint.ListFeatures
{
    public class ListFeatureSetting : ListFeatureObject
    {
        public enum Keys
        {
            Value
        }

        public string Name;

        public ListFeatureSetting(string name)
        {
            Name = name;
        }

        #region Creation
        public static ListFeatureSetting Create(string name, ListFeatureDefinition featdef)
        {
            ListFeatureSetting setting = new ListFeatureSetting(name);
            setting.FeatureDefinition = featdef;

            return setting;
        }

        public static ListFeatureSetting Create(string name, ListFeature feature)
        {
            ListFeatureSetting setting = Create(name, feature.Definition);

            if (setting != null)
                setting.Feature = feature;

            return setting;
        }
        #endregion // Creation


        #region Overrides
        public override string FullKey
        {
            get { return ListFeatureKeyManager.GenerateDottedKey("Settings", Key); }
        }

        public override string Key
        {
            get { return Name; }
        }
        #endregion // Overrides

        public string Value
        {
            get { return GetElement(ListFeatureSetting.Keys.Value); }
            set { SetElement(ListFeatureSetting.Keys.Value, value); }
        }

        public string DefaultValue
        {
            get { return GetDefaultElement(ListFeatureSetting.Keys.Value); }
        }
    }

    public class ListFeatureSettings : ListFeatureObject
    {
        Dictionary<string, ListFeatureSetting> settings;

        public ListFeatureSettings()
        {
            settings = new Dictionary<string, ListFeatureSetting>();
        }

        #region Creation
        public static ListFeatureSettings Create(ListFeatureDefinition definition)
        {
            ListFeatureSettings settings = new ListFeatureSettings();
            settings.FeatureDefinition = definition;

            if (definition.Exists)
            {
                SPFeaturePropertyCollection properties = definition.FeatureDefinition.Properties;

                foreach (SPFeatureProperty property in properties)
                {
                    string propertyName = property.Name;
                    if (settings.IdentityKeyManager.IsValidKey(propertyName))
                    {
                        string name = settings.IdentityKeyManager.ExtractInternalName(propertyName);

                        if (!settings.Contains(name))
                            settings.Add(name, definition);
                    }
                }
            }
            return settings;
        }

        public static ListFeatureSettings Create(ListFeature feature)
        {
            ListFeatureSettings settings = new ListFeatureSettings();
            settings.FeatureDefinition = feature.Definition;
            settings.Feature = feature;

            if (feature.Definition.Exists)
            {
                SPFeaturePropertyCollection properties = feature.Definition.FeatureDefinition.Properties;

                foreach (SPFeatureProperty property in properties)
                {
                    string propertyName = property.Name;
                    if (settings.IdentityKeyManager.IsValidKey(propertyName))
                    {
                        string name = settings.IdentityKeyManager.ExtractInternalName(propertyName);

                        if (!settings.Contains(name))
                            settings.Add(name, feature);
                    }
                }
            }
            return settings;
        }
        #endregion // Creation



        public ListFeatureSetting Add(string name, ListFeatureDefinition definition)
        {
            ListFeatureSetting setting = ListFeatureSetting.Create(name, definition);
            if (setting != null)
            {
                this.settings.Add(name, setting);
            }
            return setting;
        }

        public ListFeatureSetting Add(string name, ListFeature feature)
        {
            ListFeatureSetting setting = ListFeatureSetting.Create(name, feature);
            if (setting != null)
            {
                this.settings.Add(name, setting);
            }
            return setting;
        }

        public List<ListFeatureSetting> Get()
        {
            return new List<ListFeatureSetting>(settings.Values);
        }

        public ListFeatureSetting Get(ListFeatureSetting toLookFor)
        {
            foreach (ListFeatureSetting setting in settings.Values)
            {
                if (setting.Key == toLookFor.Key)
                    return setting;
            }
            return null;
        }

        public ListFeatureSetting Get(string name)
        {
            return settings[name];
        }

        public bool Contains(string name)
        {
            return settings.ContainsKey(name);
        }

        #region Overrides
		public override string FullKey
        {
            get { return "Settings"; }
        }

        public override string Key
        {
            get { return string.Empty; }
        }
        #endregion // Overrides
    }
}
